<template>
	<view class="container">
		<com-head textTit="商品评价" :typeVal="1" :scrollTop="scrollTop"></com-head>
		
		<view class="projectDetails" v-if="details.id">
			<view class="projectInfo">
				<image :src="details.goods_images[0]" mode=""></image>
				<view class="info">
					<view class="title">{{details.goods_name}}</view>
					<view class="time">{{details.sku_text}}</view>
					<!-- <view class="time store">购买门店：正大广场店</view> -->
					<view class="bottom">
						<text class="price">¥{{details.orders_payable}}</text>
						<text class="num">x {{details.number}}</text>
					</view>
				</view>
			</view>
			<!-- 评分 -->
			<view class="evalWrap">
				<view class="evalList">
					<text class="evalTit">商品评分：</text>
					<u-rate :count="5" v-model="form.goods_score" inactive-color="#E0E0E0" 
					inactive-icon="star-fill"
					active-color="#E8C778" size="34"></u-rate>
				</view>
			</view>
			<!--  -->
			<view class="tagWrap" >
				<view class="tagList" @click="chioseTag(ele) " :class="ele.isCheck ? 'active'  : '' " v-for="(ele,index) in labelList" :Key="index">
					{{ele.title}}
				</view>
			</view>
			<textarea  v-model="form.content"  placeholder="从多个角度评价宝贝,可以帮助更多需要的人" placeholder-style="color:#999999;font-size:24rpx"/>
		</view>
		<!-- 上传图片 -->
		<view class="uploadImg">
			<view class="tit">上传照片</view>
			<view class="imgbox" >
				<image v-if="imgbox.length < 6" @click="uploadPhoto()" src="//jiudao123.oss-cn-shanghai.aliyuncs.com/store/img/89765.png" mode="aspectFill"></image>
				<view class="imgWrap" v-for="(ele,index) in imgbox" :key="index" v-if="imgbox.length">
					<u-icon name="close-circle" color="#D5BE82" size="28" @click="deleImg(index)"></u-icon>
					<image :src="ele" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="button">
			<view class="common" @click="utlis.backPage()">返回</view>
			<view class="common bg" @click="evaluate()" >发布</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				value:2,
				tag:[{title:"好评",isCheck:false,type:1},{title:"质量",isCheck:false,type:4},{title:"价格",isCheck:false,type:5}],
				id:'',
				details:{},
				imgbox:[],
				form:{
					orders_goods_id:'',
					content:'',
					files:'',
					goods_score:5,
					evaluate_label_id:'',
					store_id:'',
					goods_id:''
				},
				labelList:[],
				scrollTop: 0
			}
		},
		onPageScroll(e){
			this.scrollTop = e.scrollTop
		},
		async onLoad({id}) {
			this.id = id
			this.form.orders_goods_id = id
			this.getDetalis()
			this.getEvaluate()
		},
		methods: {
			// 获取评论标签
			async getEvaluate(){
				let {data,code} = await this.$u.api.getEvaluate();
				data.forEach((ele)=>{
					ele.isCheck = false
				})
				this.labelList = data
			},
			deleImg(index){
				uni.showModal({
				    title: '提示',
					confirmColor:'#007664',
				    content: '您确定要删除此图片？',
				    success: async (res) => {
				        if (res.confirm) {
				            this.imgbox.splice(index,1)
							this.utlis.Toast('删除成功',"success")
				        } else if (res.cancel) {
				        }
				    }
				});
			},
			// 获取详情
			async getDetalis(){
				let {data,code} = await this.$u.api.getComment({id:this.id,type:2});
				if(code){
					data.goods_images = data.goods_images.split(",")
					this.details = data
				}
			},
			// 上传文件
		uploadPhoto(){
			let that = this
			wx.chooseImage({
				count:6,
				success: (imgRes) => {
					console.log(imgRes)
					let tempFilePaths = imgRes.tempFilePaths
					for (var i=0;i<tempFilePaths.length;i++){
					uni.showLoading({
						title: '文件上传中',
						mask: true,
					   });
					  uni.uploadFile({
						url: that.utlis.baseUrl + '/upload',
						filePath: imgRes.tempFilePaths[i],
						name: 'files',
						headers: {
							'Content-Type': 'multipart/form-data' //********划重点
						 },
						success(res) {
							var resdata = JSON.parse(res.data)
							that.utlis.Toast("上传成功","success")
							uni.hideLoading();
							that.imgbox.push(resdata.data)
							console.log(that.imgbox)
						},
						fail: (err) => {
							uni.hideLoading();
							that.utlis.Toast("上传失败")
						}
					})
				 }
				}
			})
		},
			
			// 评价
			async evaluate(){
				let arr = []
				this.labelList.forEach((ele)=>{
					if(ele.isCheck){
						arr.push(ele.id)
					}
				})
				if(!arr.length){
					this.utlis.Toast("请选择评论标签")
					return
				}else{
					this.form.evaluate_label_id = arr.join(",")
				}
				if(!this.form.content){
					this.utlis.Toast("请输入评论内容")
					return
				}
				this.form.store_id = this.store_id
				this.form.files = this.imgbox.join(',')
				this.form.goods_id = this.details.goods_id
				let {data,code} = await this.$u.api.setComment(this.form);
				if(code){
					this.utlis.goUrl(`/pages/mine/submitSuccess?type=3`)
					this.utlis.Toast("发布成功","success")
				}
			},
			chioseTag(ele){
				ele.isCheck  = !ele.isCheck
				// this.activeindex = index
				// if(index == 2){
				// 	this.form.type = 4
				// }else{
				// 	this.form.type = index+1
				// }
				// this.form.content = ''
			}
		}
	}
</script>

<style lang="scss" scoped >
.container{
	padding: 50rpx 32rpx 0 32rpx;
	.button{
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 116rpx;
		left: 50%;
		transform: translate(-50%);
		.common{
			width: 310rpx;
			line-height: 100rpx;
			border: 1rpx solid #007664;
			border-radius: 48rpx;
			color: #007664;
			font-size: 32rpx;
			text-align: center;
		}
		.bg{
			border: none;
			background: linear-gradient(135deg, #50DCC2 0%,  #007664);
			color: #FFFFFF;
			margin-left: 40rpx;
		}
		
	}
	.rewardWrap{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 30rpx;
		.rewardLeft{
			display: flex;
			justify-content: space-between;
			width: 509rpx;
			height: 97rpx;
			background: #ffffff;
			border-radius: 8rpx;
			padding: 0 19rpx 0 32rpx;
			color: #333333;
			.left{
				display: flex;
				align-items: center;
			}
			.tit{
				font-size: 26rpx;
				margin-right: 37rpx;
			}
			.name{
				font-size: 24rpx;
			}
		}
		.rewardBtn{
			width: 145rpx;
			line-height: 96rpx;
			background: #ffffff;
			border-radius: 8rpx;
			text-align: center;
			color: #007664;
			font-size: 24rpx;
		}
	}
	.uploadImg{
		padding: 32rpx 32rpx 32rpx 32rpx;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 30rpx;
		.tit{
			color: #333333;
			font-size: 26rpx;
			margin-bottom: 28rpx;
		}
		.imgbox{
			display: flex;
			flex-wrap: wrap;
			.imgWrap{
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
				margin-bottom: 22rpx;
				position: relative;
				u-icon{
					position: absolute;
					right: 0;
					top: 0;
				}
			}
			image{
				width: 140rpx;
				height: 140rpx;
				background: #f6f7fa;
			}
			.imgWrap:nth-child(4n+4){
				margin-right: 0;
			}
		}
	}
	.projectDetails{
		width: 686rpx;
		background: #ffffff;
		border-radius: 20rpx;
		padding: 32rpx 32rpx 27rpx 32rpx;
		box-sizing: border-box;
		textarea{
			width: 89%;
			padding: 16rpx 33rpx;
			background-color: #F6F7FA;
			color: #333333;
			height: 204rpx;
			border-radius: 4rpx;
		}
		.tagWrap{
			margin: 32rpx 0 20rpx 0;
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			.tagList{
				padding: 0 14rpx;
				// width: 118rpx;
				line-height: 52rpx;
				background: rgba(194,194,194,.1);
			    color: #666666;
			    font-size: 24rpx;
				margin-right: 20rpx;
				text-align: center;
				margin-bottom: 20rpx;
				border-radius: 6rpx;
			}
			.active{
				color: #007664;
				background-color:rgba(136,29,3,.1);
			}
		}
		.evalWrap{
			.evalList{
				display: flex;
				align-items: center;
				color: #333333;
				font-size: 24rpx;
				margin-bottom: 24rpx;
				.evalTit{
					margin-right: 16rpx;
				}
			}
		}
		.projectInfo{
			display: flex;
			margin-bottom: 42rpx;
			image{
				width: 161rpx;
				height: 161rpx;
				border-radius: 20rpx;
				flex: none;
				margin-right: 24rpx;
			}
			.info{
				.title{
					font-size: 32rpx;
					color: #000000;
					line-height: 45rpx;
					margin-bottom: 10rpx;
				}
				.time{
					font-size: 24rpx;
					color: #999999;
					line-height: 26rpx;
					margin-bottom: 19rpx;
				}
				.store{
					margin-bottom: 6rpx;
				}
				.bottom{
					width: 440rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					color: #333333;
					font-size: 28rpx;
					line-height: 40rpx;
					.price{
						color: #007664;
					}
				}
			}
		}
	}
}
</style>
<style >
page{
	background-color: #F6F7FA;
}
</style>